/*
 *  DataRepository.h
 *  MetaServer
 *
 *  Created by Ian Pye on 1/7/08.
 *  Copyright 2008 TriceratopsPowered. All rights reserved.
 *
 */

#ifndef DATA_REP_HEADER
#define DATA_REP_HEADER 1

#include <errno.h>
#include <map>
#include <semaphore.h>
#include <db_cxx.h>

namespace ringer{

static const std::string PERMISSIONS_SEM_NAME = "/RingerPermissions";
static const std::string METADATA_LOGGING_CONFIG = "mds_logging_config";
static const std::string RINGER_METADATA_NAME = "METADATA_SERVER";
static log4cxx::LoggerPtr g_metaserver_logger(log4cxx::Logger::getLogger(RINGER_METADATA_NAME));    

class DataRepository {

public:
  DataRepository(std::string db_env, std::string db_name);
  ~DataRepository();
  int get(const rpath_t&, RNode&);
  bool put(const RNode&);
  bool r_delete(const rpath_t&);
  
  // permissions
  int get_read(const rpath_t& path, const UserInfo& userInfo); 
  int get_write(const rpath_t& path, const UserInfo& userInfo);
  bool end_read(const rpath_t& path, const UserInfo& userInfo);
  bool end_write(const rpath_t& path, const UserInfo& userInfo);
  
private:
  std::map<rpath_t, RNode> m_files;
  std::set<int32_t> m_users;
  std::set<int32_t> m_groups;
  
  // needed for serialization
  boost::shared_ptr<facebook::thrift::protocol::TProtocolFactory>* mp_protocolFactory;
  
  // synchronize access
  sem_t *pm_permissions_sem;
  
  // Persistent Storage
  Db::Db* mds_db;

};

} // end namespace ringer
#endif
